package bus.view;
import java.awt.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.Serializable;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.*;

import bus.dao.TicketDao;
import bus.util.DButil;
public class Ticket_Info extends JFrame implements ItemListener,ActionListener{
	static JFrame jf;
	JPanel jp;
	JLabel j1,j2,j3,j4,j5,j6;
	JComboBox jc;
	String a1=" ",l,yao;
	String aa[];
	int f;
	public String user_name;
	JButton jb;
	Ticket_Info(int f, String username)
	{ 
		this.user_name=username;
		this.f=f;
		jb=new JButton("Confirm");
		  jb.setBounds(70,300,70,30);
		  jb.setFont(new java.awt.Font("Dialog",1, 15));
		jc=new JComboBox();
		jf=new JFrame();
		jp=new JPanel();
		j1=new JLabel("TicketNo  ");
		j2=new JLabel("Destination  ");
		j3=new JLabel("Seats   ");
		j4=new JLabel("Username   ");
		j5=new JLabel("date_of_journey   ");
		j6=new JLabel("pickup_point   ");
		j1.setFont(new java.awt.Font("Dialog",1, 18));
		j2.setFont(new java.awt.Font("Dialog",1, 18));
		j3.setFont(new java.awt.Font("Dialog",1, 18));
		j4.setFont(new java.awt.Font("Dialog",1, 18));
		j5.setFont(new java.awt.Font("Dialog",1, 18));
		j6.setFont(new java.awt.Font("Dialog",1, 18));
		j1.setBounds(10,10,80,35);
		
		jp.setSize(800,400);
		jc.setPreferredSize(new Dimension(700, 30));
		jc.addItemListener(this);
		jp.add(j5);
		jp.add(j3);
		jp.add(j4);
		jp.add(j6);
		jp.add(j2);
		jp.add(j1);
		jp.add(jc);
		jp.add(jb);
		jb.addActionListener(this);
		//jp.setLayout(null);
		add(jp);
		setTitle("Choose Ticket");
		 setBounds(250,100,800,400);
	    setLayout(null);
	    setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Ticket_Info lo=new Ticket_Info(0, "  ");
    	String w=lo.search();
	}
	
	public String search() {
		 jc.setSelectedIndex(-1);
		//jc.addItem("   Choose  ");
		// TODO Auto-generated method stub
		try
	       {
			   DButil db=new DButil();
			   Connection con=db.getCon();
			   TicketDao td=new TicketDao();
			   
			   ResultSet i=td.ticketSelect(con);
			   
	     while(i.next())
		{
	    	 if(i.getString("username").equals(user_name)) {
			 jc.addItem("             "+str(i.getString(1),30," ")+str(i.getString(3),30," ")+str(i.getString(2),25," ")+str(i.getString(4),25," ")+str(i.getString(5),20," ")+str(i.getString(6),30," "));
		}
	    	 }
	       }
		catch(Exception e)
		{
			System.out.println(e);
		}
		return aa[0];
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		   
		 if (e.getStateChange() == ItemEvent.SELECTED)
		 {
          a1=jc.getSelectedItem().toString().trim();  
		 }
		 aa=new String[a1.split("\\ ").length];
		    
		 aa=a1.split("\\ ");
		    if(f==1) {
		    	trip ll=new trip();
		        ll.qie(aa[0]);
		    }
		    else {
		    	trip2 t2=new trip2();
		    	t2.qie(aa[0]);
		    }
		   
	}
	public void actionPerformed(ActionEvent e) {
		  // TODO Auto-generated method stub
		  setVisible(false);
		 }
	public static String str(String str1, int lenth, String st2) throws Exception {
		StringBuilder strb1 = new StringBuilder(str1);
		lenth = lenth-str1.length();
		while (lenth >= 0) {
			lenth--;
			strb1.append(st2);
		}
		return strb1.toString();
	}

	}
